//
//  LGDoor.m
//  ALS3.0
//
//  Created by guomianxiang on 16/12/30.
//  Copyright © 2016年 guomianxiang. All rights reserved.
//

#import "LGDoor.h"
#import "NSString+LG.h"
//#import "PinYin4Objc.h"

@implementation LGDoor

- (instancetype)init {
    if (self = [super init]) {
        //默认频率为1 避免为0导致频率算法无效
        self.frequency = 1;
        
        self.favorite = 0;
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        self.doorName = [aDecoder decodeObjectForKey:@"doorName"];
//        self.pinyin = [aDecoder decodeObjectForKey:@"pinyin"];

        self.ip = [aDecoder decodeObjectForKey:@"ip"];
        self.port = [aDecoder decodeObjectForKey:@"port"];
        
        NSNumber *number;
        
        number = [aDecoder decodeObjectForKey:@"doorIndex"];
        self.doorIndex = [number integerValue];
        
        number = [aDecoder decodeObjectForKey:@"valid"];
        self.valid = [number boolValue];
        
        number = [aDecoder decodeObjectForKey:@"show"];
        self.show = [number boolValue];
        
        number = [aDecoder decodeObjectForKey:@"frequency"];
        self.frequency = [number unsignedIntegerValue];

        number = [aDecoder decodeObjectForKey:@"favorite"];
        self.favorite = [number integerValue];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.doorName forKey:@"doorName"];
//    [aCoder encodeObject:self.pinyin forKey:@"pinyin"];
    [aCoder encodeObject:self.ip forKey:@"ip"];
    [aCoder encodeObject:self.port forKey:@"port"];

    NSNumber *number = @(self.doorIndex);
    [aCoder encodeObject:number forKey:@"doorIndex"];
    
    number = @(self.valid);
    [aCoder encodeObject:number forKey:@"valid"];
    number = @(self.show);
    [aCoder encodeObject:number forKey:@"show"];

    
    number = @(self.frequency);
    [aCoder encodeObject:number forKey:@"frequency"];
    
    number = @(self.favorite);
    [aCoder encodeObject:number forKey:@"favorite"];
}

- (void)setDoorName:(NSString *)doorName {

    _doorName = doorName;
    
    if (doorName == nil) {
        return;
    }
    _pinyin = [doorName stringTransformPinYin];
}
- (NSString *)description {
//    return [NSString stringWithFormat:@"name: %@-pinyin:%@-valid:%i-show:%i-doorindex:%lu-%@",_doorName,_pinyin,_valid,_show,_doorIndex,_ip];
    return [NSString stringWithFormat:@"---  %li,%@",_favorite,_doorName];
}

- (id)copyWithZone:(nullable NSZone *)zone {
    LGDoor *doorItem = [[LGDoor alloc]init];
    doorItem.doorName = self.doorName;
    doorItem.pinyin = self.pinyin;
    doorItem.doorIndex = self.doorIndex;
    doorItem.controllerIndex =self.controllerIndex;
    doorItem.ip = self.ip;
    doorItem.port = self.port;
    doorItem.valid = self.valid;
    doorItem.show = self.show;
    doorItem.frequency = self.frequency;
    doorItem.favorite = self.favorite;
    return doorItem;
}

- (void)setFavorite:(NSInteger)favorite {
    if (favorite > 0) _favorite = 1;
    if (favorite < 0) _favorite = -1;
    if (favorite == 0) _favorite = 0;
}

@end
